﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Data;
using System.Text;

namespace Infrastructure.Utility
{
    public class Common
    {
        public static string SpaceText(int length)
        {
            if (length == 0) length = 1;
            if (HttpContext.Current == null)
            {
                if (length > 1) return SpaceText(length - 1) + " ";
                else return " ";
            }
            else
            {
                if (length > 1) return SpaceText(length - 1) + "&nbsp;";
                else return "&nbsp;";
            }
        }

        public static Color HSLtoRGB(double h, double s, double l)
        {
            double r = 0, g = 0, b = 0;
            double temp1, temp2;
            if (l == 0)
            {
                r = g = b = 0;
            }
            else
            {
                if (s == 0)
                {
                    r = g = b = l;
                }
                else
                {
                    temp2 = ((l <= 0.5) ? l * (1.0 + s) : l + s - (l * s));
                    temp1 = 2.0 * l - temp2;
                    double[] t3 = new double[] { h + 1.0 / 3.0, h, h - 1.0 / 3.0 };
                    double[] clr = new double[] { 0, 0, 0 };
                    for (int i = 0; i < 3; i++)
                    {
                        if (t3[i] < 0)
                            t3[i] += 1.0;
                        if (t3[i] > 1)
                            t3[i] -= 1.0;
                        if (6.0 * t3[i] < 1.0)
                            clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
                        else if (2.0 * t3[i] < 1.0)
                            clr[i] = temp2;
                        else if (3.0 * t3[i] < 2.0)
                            clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
                        else
                            clr[i] = temp1;
                    }
                    r = clr[0];
                    g = clr[1];
                    b = clr[2];
                }
            }
            return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
        }

        public static string BuildReportHeadHtml(params string[] args)
        {
            StringBuilder builder = new StringBuilder("<br/><p>");
            for (int i = 0; i < args.Length; i++)
                if (!string.IsNullOrEmpty(args[i]))
                    builder.AppendFormat("{0}<br/>", args[i]);
            builder=builder.Remove(builder.Length - 5, 5);
            builder.Append("<p/>");
            return builder.ToString();
        }
        /// <summary>
        /// if the field type is ut_rq16 in database,you can use the function to format it into sql command.
        /// </summary>
        /// <param name="field">field name in database.</param>
        /// <returns>standrad datetime string</returns>
        public static string FormatDateTimeField(string field)
        {
            return string.Format("convert(datetime,substring(replace({0},':',''),1,4)+'-'+substring(replace({0},':',''),5,2)+'-'+substring(replace({0},':',''),7,2)+' '+substring(replace({0},':',''),9,2)+':'+substring(replace({0},':',''),11,2)+':'+substring(replace({0},':',''),13,2)) as [{0}]", field);
        }
    }
}
